package com.iotechn.unimall.admin.api.goods;/*
@PackageName:com.iotechn.unimall.admin.api.goods
@ClassName: AdminGroupShopGoodsServiceImpl
@Description:
@author kbq
@date 19-11-13下午4:21
*/

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iotechn.unimall.core.annotation.HttpParam;
import com.iotechn.unimall.core.annotation.HttpParamType;
import com.iotechn.unimall.core.annotation.param.NotNull;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.ShopOneDO;
import com.iotechn.unimall.data.domain.SpuDO;
import com.iotechn.unimall.data.domain.UserShopDO;
import com.iotechn.unimall.data.dto.goods.ShopOneDTO;
import com.iotechn.unimall.data.enums.StatusType;
import com.iotechn.unimall.data.mapper.ShopOneMapper;
import com.iotechn.unimall.data.mapper.SkuMapper;
import com.iotechn.unimall.data.mapper.SpuMapper;
import com.iotechn.unimall.data.mapper.UserShopMapper;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@EnableScheduling
public class AdminShopOneServiceImpl implements AdminShopOneService {



    @Autowired
    private ShopOneMapper shopOneMapper;
    @Autowired
    private UserShopMapper userShopMapper;

    @Autowired
    private CacheComponent cacheComponent;

    private static final String GROUP_SHOP_CACHE = "CA_SHOP_ONE";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addShopOne(Long adminId, Long shopId,String qrCode,String address,Integer type,String lgla,String img) throws ServiceException {

        Date now = new Date();
        ShopOneDO shopOneDO = new ShopOneDO();
        shopOneDO.setShopId(shopId);
        UserShopDO userShopDO=userShopMapper.selectById(shopId);
        shopOneDO.setShopName(userShopDO.getShopName());
        shopOneDO.setDakaName(SessionUtil.getAdmin().getRealname());
        shopOneDO.setCompanyName(userShopDO.getJianlidanwei());
        shopOneDO.setAdminId(adminId);
        shopOneDO.setQrcode(qrCode);
        shopOneDO.setGmtCreate(now);
        shopOneDO.setGmtUpdate(now);
        shopOneDO.setAddress(address);
        shopOneDO.setType(2);
        shopOneDO.setStatus(1);
        shopOneDO.setImg(img);
        shopOneDO.setLgla(lgla);
        // 3.设置是否在活动时间的状态
        if (shopOneMapper.insert(shopOneDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.GROUP_SHOP_SPU_ADD_SQL_QUERY_ERROR);
        }
         return "ok";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteShopOne(Long adminId, Long id) throws ServiceException {

        ShopOneDO shopOneDO = shopOneMapper.selectById(id);
        if (shopOneDO == null) {
            throw new AdminServiceException(ExceptionDefinition.SPU_NO_EXITS_OR_ONLY_SPU);
        }


        if (shopOneMapper.deleteById(id) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.GROUP_SHOP_SPU_DELETE_SQL_QUERY_ERROR);
        }
        cacheComponent.delPrefixKey(GROUP_SHOP_CACHE);
        return "ok";
    }


    @Override
    public Page<ShopOneDTO> queryShopOneNew(Long adminId, String shopName, Long shopId,Integer type, Integer page, Integer pageSize) throws ServiceException {
        Integer count = shopOneMapper.getShopOnePageCount(shopName,adminId, shopId,type);
        List<ShopOneDTO> groupShopPage = shopOneMapper.getShopOnePage(shopName,adminId, shopId,type, (page - 1) * pageSize, pageSize);
        return new Page<>(groupShopPage, page, pageSize, count);

    }

    @Override
    public Page<ShopOneDTO> queryShopOne(Long adminId, Long id, String shopName, Integer status, Integer page, Integer limit) throws ServiceException {
        EntityWrapper<ShopOneDO> groupShopSpuDOEntityWrapper = new EntityWrapper<ShopOneDO>();
        if (id != null) {
            groupShopSpuDOEntityWrapper.eq("id", id);
        }

        if (status != null) {
            groupShopSpuDOEntityWrapper.eq("status", status);
        }

        if (shopName != null) {
            EntityWrapper<UserShopDO> spuDOEntityWrapper = new EntityWrapper<>();
            spuDOEntityWrapper.setSqlSelect("id");
            spuDOEntityWrapper.like("shop_name", shopName);
            List<Object> objectList = userShopMapper.selectObjs(spuDOEntityWrapper);
            List<String> collect = objectList.stream().map(s -> String.valueOf(s)).collect(Collectors.toList());
            groupShopSpuDOEntityWrapper.in("shop_id", collect);
        }

        List<ShopOneDO> groupShopDOList = shopOneMapper.selectPage(new RowBounds((page - 1) * limit, limit), groupShopSpuDOEntityWrapper);
        Integer count = shopOneMapper.selectCount(groupShopSpuDOEntityWrapper);
        List<ShopOneDTO> groupShopDTOList = new LinkedList<>();

        for (ShopOneDO groupShopDO : groupShopDOList) {
            ShopOneDTO groupShopDTO = new ShopOneDTO();
            UserShopDO spuDO = userShopMapper.selectById(groupShopDO.getShopId());
            BeanUtils.copyProperties(spuDO, groupShopDTO);
            BeanUtils.copyProperties(groupShopDO, groupShopDTO);
            groupShopDTOList.add(groupShopDTO);
        }
        return new Page<ShopOneDTO>(groupShopDTOList, page, limit, count);
    }


    private void setShopOneStatus(Date now, SpuDO spuDO, ShopOneDO groupShopDO) {
        groupShopDO.setStatus(StatusType.LOCK.getCode());

    }
    @Override
    public List<ShopOneDTO> queryQiandao(
            Long adminId,
            Long shopId,
            Integer  type){
         type=2;
         if(SessionUtil.getAdmin().getRoleIds().contains(3) || SessionUtil.getAdmin().getRoleIds().contains(2)){
             type=1;
         }
         return shopOneMapper.selectQianDao(shopId,type,adminId);
    }


}
